package com.example.notificationtest.slice;

import com.example.notificationtest.ResourceTable;
import com.example.notificationtest.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.event.intentagent.IntentAgent;
import ohos.event.intentagent.IntentAgentHelper;
import ohos.event.intentagent.IntentAgentInfo;
import ohos.event.intentagent.IntentAgentConstant.Flags;
import ohos.event.intentagent.IntentAgentConstant.OperationType;
import ohos.event.notification.*;
import ohos.event.notification.NotificationRequest.*;
import ohos.event.notification.NotificationRequest.NotificationConversationalContent.ConversationalMessage;

import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;
import ohos.miscservices.timeutility.Time;
import ohos.rpc.RemoteException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private Button mBtnPublishNotification;
    private Button mBtnPublishNotificationOpenAbility;
    private Button mBtnPublishNotificationWithSlot;
    private Button mBtnPublishNotificationLongtext;
    private Button mBtnPublishNotificationPicture;
    private Button mBtnPublishNotificationMultiline;
    private Button mBtnPublishNotificationConversational;
    private Button mBtnPublishNotificationMedia;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mBtnPublishNotification = (Button) findComponentById(ResourceTable.Id_btn_publish_notification);
        mBtnPublishNotification.setClickedListener(this);
        mBtnPublishNotificationOpenAbility = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_open_ability);
        mBtnPublishNotificationOpenAbility.setClickedListener(this);
        mBtnPublishNotificationWithSlot = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_with_slot);
        mBtnPublishNotificationWithSlot.setClickedListener(this);
        mBtnPublishNotificationLongtext = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_longtext);
        mBtnPublishNotificationLongtext.setClickedListener(this);
        mBtnPublishNotificationPicture = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_picture);
        mBtnPublishNotificationPicture.setClickedListener(this);
        mBtnPublishNotificationMultiline = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_multiline);
        mBtnPublishNotificationMultiline.setClickedListener(this);
        mBtnPublishNotificationConversational = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_conversational);
        mBtnPublishNotificationConversational.setClickedListener(this);
        mBtnPublishNotificationMedia = (Button) findComponentById(ResourceTable.Id_btn_publish_notification_media);
        mBtnPublishNotificationMedia.setClickedListener(this);

        // 创建通知渠道
        NotificationSlot slot = new NotificationSlot("slot1", "一般性通知", NotificationSlot.LEVEL_DEFAULT);
        slot.setDescription("一般性通知");

        NotificationSlot slot2 = new NotificationSlot("slot2", "特别重要通知", NotificationSlot.LEVEL_HIGH);
        slot2.setDescription("特别重要通知");
        // 震动提醒
        slot2.setEnableVibration(true);
        // 锁屏通知
        slot2.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);
        // 绕过免打扰模式
        slot2.enableBypassDnd(true);
        // 开启呼吸灯提醒
        slot2.setEnableLight(true);
        // 设置呼吸灯的提醒颜色
        slot2.setLedLightColor(Color.RED.getValue());

        try {
            NotificationHelper.addNotificationSlot(slot);
            NotificationHelper.addNotificationSlot(slot2);
        } catch (RemoteException e) {
            Utils.log("加入通知渠道失败: " + e.getLocalizedMessage());
        }
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        // 发布普通通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification) {
            // 普通文本通知内容
            NotificationNormalContent content = new NotificationNormalContent()
                    .setTitle("标题Title")
                    .setText("内容Text")
                    .setAdditionalText("次要内容AdditionalText");
            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1001)
                    .setContent(notificationContent);
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 发布普通通知(单击打开Ability)
        if (component.getId() == ResourceTable.Id_btn_publish_notification_open_ability) {

            // 创建IntentAgent对象
            IntentAgent agent = createOpenThisAbilityIntentAgent();
            NotificationNormalContent content = new NotificationNormalContent()
                    .setTitle("NotificationTest")
                    .setText("单击打开主界面");
            NotificationContent notificationContent = new NotificationContent(content);
            NotificationRequest request = new NotificationRequest(1002)
                    .setContent(notificationContent)
                    .setIntentAgent(agent);
            try {
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 通过渠道发布通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification_with_slot) {
            // 普通文本通知内容
            NotificationNormalContent content = new NotificationNormalContent()
                    .setTitle("重要通知")
                    .setText("通知内容");
            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1003)
                    .setContent(notificationContent)
                    .setSlotId("slot2");
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 发布长文本通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification_longtext) {
            // 长文本通知内容
            NotificationLongTextContent content = new NotificationLongTextContent()
                    .setTitle("标题Title")
                    .setExpandedTitle("扩展标题ExpandedTitle")
                    .setAdditionalText("次要内容AdditionalText")
                    .setLongText("长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText长文本LongText");
            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1004)
                    .setContent(notificationContent);
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 发布多行通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification_multiline) {

            NotificationMultiLineContent content = new NotificationMultiLineContent()
                    .setTitle("标题Title")
                    .setText("内容Text")
                    .setExpandedTitle("扩展标题ExpandedTitle")
                    .addSingleLine("行1内容")
                    .addSingleLine("行2内容")
                    .addSingleLine("行3内容");
            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1005)
                    .setContent(notificationContent);
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 发布图片通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification_picture) {
            // 图片通知内容
            NotificationPictureContent content = new NotificationPictureContent()
                    .setTitle("标题Title")
                    .setText("内容Text")
                    .setBriefText("简介文本BriefText")
                    .setExpandedTitle("扩展标题ExpandedTitle")
                    .setBigPicture(getPixelMap(ResourceTable.Media_testpicture));
            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1006)
                    .setContent(notificationContent);
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 发布社交通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification_conversational) {
            // 消息用户1
            MessageUser user1 = new MessageUser();
            user1.setName("董昱");
            user1.setPixelMap(getPixelMap(ResourceTable.Media_profile_1));
            // 消息用户2
            MessageUser user2 = new MessageUser();
            user2.setName("王娜");
            user2.setPixelMap(getPixelMap(ResourceTable.Media_profile_2));
            // 社交通知内容
            NotificationConversationalContent content = new NotificationConversationalContent(user1)
                    .setConversationTitle("即时消息")
                    .addConversationalMessage(
                            new ConversationalMessage("你好呀!"
                                    , Time.getCurrentTime(), user1))
                    .addConversationalMessage(
                            new ConversationalMessage("在么帅哥!"
                                    , Time.getCurrentTime(), user2));

            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1007)
                    .setContent(notificationContent);
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
        // 发布媒体通知
        if (component.getId() == ResourceTable.Id_btn_publish_notification_media) {

            // 媒体通知内容
            NotificationMediaContent content = new NotificationMediaContent()
                    .setTitle("标题Title")
                    .setText("内容Text")
                    .setAdditionalText("次要内容AdditionalText")
                    .setShownActions(new int[]{0});
            // 定义3个按钮
            NotificationActionButton button = new NotificationActionButton.Builder(
                    getPixelMap(ResourceTable.Media_play),
                    "开始", null).build();
            NotificationActionButton button2 = new NotificationActionButton.Builder(
                    getPixelMap(ResourceTable.Media_like),
                    "喜欢", null).build();
            NotificationActionButton button3 = new NotificationActionButton.Builder(
                    getPixelMap(ResourceTable.Media_star),
                    "收藏", null).build();

            // 创建NotificationContent通知内容对象
            NotificationContent notificationContent = new NotificationContent(content);
            // 创建NotificationRequest通知请求对象
            NotificationRequest request = new NotificationRequest(1008)
                    .setContent(notificationContent)
                    .addActionButton(button)
                    .addActionButton(button2)
                    .addActionButton(button3);
            try {
                // 发布通知
                NotificationHelper.publishNotification(request);
            } catch (RemoteException e) {
                Utils.log("发布通知异常: " + e.getLocalizedMessage());
            }
        }
    }

    private IntentAgent createOpenThisAbilityIntentAgent() {

        // 创建打开Ability的Intent对象
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.notificationtest")
                .withAbilityName("com.example.notificationtest.MainAbility")
                .build();
        Intent intent = new Intent();
        intent.setOperation(operation);
        // 将Intent对象添加到List<Intent>对象中
        List<Intent> intents = new ArrayList<>();
        intents.add(intent);

        // 创建flags对象
        List<Flags> flags = new ArrayList<>();
        flags.add(Flags.ONE_TIME_FLAG);

        // 创建启动Ability的IntentAgentInfo对象
        IntentAgentInfo info = new IntentAgentInfo(
                200,
                OperationType.START_ABILITY,
                flags,
                intents,
                null);
        // 通过IntentAgentHelper创建IntentAgent对象
        IntentAgent agent = IntentAgentHelper.getIntentAgent(this, info);
        return agent;
    }

    /**
     * 通过资源ID获取位图对象
     **/
    private PixelMap getPixelMap(int drawableId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(drawableId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
